package com.pxxy.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/******************
 * @Author yejf
 * @Description TODO
 */
public class SqlSessionUtil {

    private static SqlSessionFactory sessionFactory;

    private static final Logger log = LoggerFactory.getLogger(SqlSessionUtil.class);

    static {
        log.debug("开始创建sqlSessionFactory....");
        try {
            sessionFactory = new SqlSessionFactoryBuilder().build(
                    Resources.getResourceAsStream("mybatis-config.xml")
            );
            log.debug("创建完成....");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSession(boolean autoCommit) {
        log.debug("创建sqlSession...");
        return sessionFactory == null ? null : sessionFactory.openSession(autoCommit);
    }

    public static SqlSession getSession() {
        return getSession(false);
    }

}
